package com.yonyou.isv.demo.openapi.base;

import com.yonyou.isv.demo.openapi.base.datacenter.DataCenterUrlProvider;
import com.yonyou.isv.demo.openapi.base.datacenter.impl.QueryDataCenterUrlProviderImpl;
import com.yonyou.isv.demo.openapi.base.module.auth.OpenApiAuthProviderFactory;
import com.yonyou.isv.demo.openapi.base.module.auth.OpenApiAuthProviderFactoryImpl;
import com.yonyou.isv.demo.openapi.base.module.auth.isv.ISVAuthProvider;
import com.yonyou.isv.demo.openapi.base.module.auth.isv.ISVCryptoService;
import com.yonyou.isv.demo.openapi.base.module.auth.isv.impl.ISVAuthProviderImpl;
import com.yonyou.isv.demo.openapi.base.module.auth.isv.impl.ISVCryptoServiceImpl;
import com.yonyou.isv.demo.openapi.base.module.auth.tenant.TenantAuthProvider;
import com.yonyou.isv.demo.openapi.base.module.auth.tenant.impl.TenantAuthProviderImpl;
import com.yonyou.isv.demo.openapi.base.module.isv.event.ISVEventListenerController;
import com.yonyou.isv.demo.openapi.base.module.isv.event.ISVReceivedEventDispatcher;
import com.yonyou.isv.demo.openapi.base.module.isv.event.impl.ISVReceivedEventDispatcherImpl;
import com.yonyou.isv.demo.openapi.base.network.OpenApiRequestEncryptor;
import com.yonyou.isv.demo.openapi.base.properties.OpenApiProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * 本类主要用于
 *
 * @author liuhaoi
 * @since Created At 2020/5/11 10:31 上午
 */
@RequiredArgsConstructor
@Configuration
@EnableConfigurationProperties(OpenApiProperties.class)
public class OpenApiAutoConfiguration {

    private final OpenApiProperties properties;

    @Bean
    @ConditionalOnMissingBean
    public OpenApiRequestEncryptor openApiRequestEncryptor() {
        return new OpenApiRequestEncryptor();
    }

    @Bean
    public TenantAuthProvider openApiAccessTokenProvider(OpenApiRequestEncryptor encryptor, RestTemplate restTemplate) {
        return new TenantAuthProviderImpl(properties, encryptor, restTemplate);
    }

    @Bean
    @ConditionalOnMissingBean
    public ISVCryptoService isvCryptoService() {
        return new ISVCryptoServiceImpl(properties);
    }

    @Bean
    @ConditionalOnMissingBean
    public ISVAuthProvider isvTokenProvider(RestTemplate restTemplate, OpenApiRequestEncryptor encryptor) {
        return new ISVAuthProviderImpl(restTemplate, properties, encryptor);
    }

    @Bean
    @ConditionalOnMissingBean
    public ISVReceivedEventDispatcher isvReceivedEventDispatcher(ApplicationContext context) {
        return new ISVReceivedEventDispatcherImpl(context);
    }

    @Bean
    @ConditionalOnMissingBean
    public ISVEventListenerController isvEventListenerController(ISVCryptoService cryptoService, ISVReceivedEventDispatcher dispatcher) {
        return new ISVEventListenerController(cryptoService, dispatcher);
    }

    @Bean
    @ConditionalOnMissingBean
    public OpenApiAuthProviderFactory openApiAuthProviderFactory(TenantAuthProvider tenantAuthProvider, ISVAuthProvider isvAuthProvider) {
        return new OpenApiAuthProviderFactoryImpl(isvAuthProvider, tenantAuthProvider);
    }

    @Bean
    @ConditionalOnMissingBean
    public DataCenterUrlProvider dataCenterUrlProvider() {
        return new QueryDataCenterUrlProviderImpl();
    }

}
